import { Module } from '@nestjs/common';
import { TypeOrmModule, TypeOrmModuleOptions } from '@nestjs/typeorm';
import { ConfigModule, ConfigService } from '@nestjs/config';
// import fs = require('fs');
// const path = require('path');
import entities from './entities';
@Module({
  imports: [
    TypeOrmModule.forRootAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: async (configService: ConfigService) => {
        // useFactory: async (configService: ConfigService) => ({
        // console.log(
        //   'configService.get<string>()',
        //   configService.get<string>('DB_HOST'),
        // );
        return {
          type: 'mysql',
          host: configService.get<string>('DB_HOST'),
          port: configService.get<number>('DB_PORT'),
          username: configService.get<string>('DB_USERNAME'),
          password: configService.get<string>('DB_PASSWORD'),
          database: configService.get<string>('DB_DATABASE'),
          // entities: ['dist/**/*.entity{.ts,.js}'],
          entities: entities,
          synchronize: true,
          timezone: '+08:00', // 东八区
          driver: require('mysql2'),
          // logging: true, // 启用日志
        };
      },
    }),
  ],
})
export class DatabaseModule {}
